<template>
  <div class="drag-drop">
    <div class="drag-drop-item">
      <h3>设计稿包含的文件 (请拖放到下面对应版权的区域)</h3>
      <div class="img-box">
        <el-image v-for="item in psdArr" :key="item" draggable="true" :src="item" class="img" fit="contain" @dragstart="drag($event, item, 'psdArr')" ></el-image>
      </div>
    </div>
    <div class="drag-drop-item" @drop="drop($event, 'qnPhoto')" @dragover="allowDrop">
      <h3>青柠图库提供图片</h3>
      <div class="img-box">
        <el-image v-for="item in qnPhoto" :key="item" :src="item" class="img" fit="contain" draggable @dragstart="drag($event, item, 'qnPhoto')"></el-image>
        <div v-show="qnPhoto.length === 0" class="region">
          可将素材拖拽到此区域
        </div>
      </div>
    </div>
    <div class="drag-drop-item" @drop="drop($event, 'ccPhoto')" @dragover="allowDrop">
      <h3>允许的CC0图库图片</h3>
      <div class="img-box">
        <el-image v-for="item in ccPhoto" :key="item" :src="item" class="img" fit="contain" draggable @dragstart="drag($event, item, 'ccPhoto')"></el-image>
        <div v-show="ccPhoto.length === 0" class="region">
          可将素材拖拽到此区域
        </div>
      </div>
    </div>
    <div class="drag-drop-item" @drop="drop($event, 'originalPhoto')" @dragover="allowDrop">
      <h3>原创素材</h3>
      <div class="img-box">
        <el-image v-for="item in originalPhoto" :key="item" :src="item" class="img" fit="contain" draggable @dragstart="drag($event, item, 'originalPhoto')"></el-image>
        <div v-show="originalPhoto.length === 0" class="region">
          可将素材拖拽到此区域
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Drap',
  data() {
    return {
      imgArr: [
        'https://cdn.qingning6.com/common/photo/c5738c80f06d46ff8d14eaaeb4960531.jpg',
        'https://cdn.qingning6.com/common/photo/68cb44789ba5482984712caccdd8ff66.jpg?x-oss-process=image/resize,h_500,m_lfit',
        'https://cdn.qingning6.com/common/photo/f83458f7b573481ab0a7873b9270378b.jpg?x-oss-process=image/resize,h_50,m_lfit',
        'https://cdn.qingning6.com/common/photo/7d5175b9c990476e8c8e6b632c240bac.jpg?x-oss-process=image/resize,h_50,m_lfit',
        'https://cdn.qingning6.com/common/photo/4adb3d58af6e4420bb77ae72cc966245.jpg',
        'https://cdn.qingning6.com/common/photo/b0c5459af8f7404880d5306d04546111.jpg',
        'https://cdn.qingning6.com/common/photo/f62942e56890494088be803bd0261bb1.jpg',
        'https://cdn.qingning6.com/common/photo/c268786e4401472e9d60d67f8916bc13.jpg',
        'https://cdn.qingning6.com/common/photo/ecd52849bbb34b889a4b17c787db5efb.jpg',
        'https://cdn.qingning6.com/common/photo/8cce5d463d4b48169b2c82e07692a9e4.jpg'
      ],
      psdArr: [],
      qnPhoto: [],
      ccPhoto: [],
      originalPhoto: []
    }
  },
  mounted() {
    this.psdArr = this.imgArr
  },
  methods: {
    drag(e, item, name) {
      e.dataTransfer.setData('data', item)
      e.dataTransfer.setData('name', name)
     /* var img = new Image()
      // img.src = item + '?x-oss-process=image/resize,h_50,m_lfit'
      img.src = item
      img.crossOrigin = ''
      console.log(item + '?x-oss-process=image/resize,h_50,m_lfit')
      e.dataTransfer.setDragImage(img, 10, 10)*/
    },
    drop(e, name) {
      const data = e.dataTransfer.getData('data')
      const getName = e.dataTransfer.getData('name')
      if (name === getName) {
        return
      }

      this[name].push(data)
      this[getName] = this[getName].filter(item => item !== data)
    },
    allowDrop(e) {
      e.preventDefault()
    }
  }
}
</script>

<style lang="scss" scoped>
.drag-drop {
  padding: 24px;
  background-color: #eee;
  //user-select: none;

  &-item {
    padding: 24px;
    background-color: #fff;
    margin-bottom: 24px;

    .img-box {
      display: flex;

      .img {
        margin: 0 10px 10px 0;
        //width: 90px;
        //height: 90px;
        width: 200px;
        height: 200px;
        background: #f5f7fa;
      }

      .region {
        width: 100%;
        height: 90px;
        border: 1px dashed #eee;
        display: flex;
        align-items: center;
        padding-left: 40px;
        font-size: 14px;
        color: #909399;
      }
    }
  }
}
</style>
